/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.PsiSearchHelper;
import com.intellij.psi.search.SearchScope;
import com.intellij.util.IncorrectOperationException;

import java.util.ArrayList;
import java.util.List;

public class UnusedFieldInspection extends LocalInspectionTool {
    public String getGroupDisplayName() {
        return "PurpleMonkeyDishwasher";
    }

    public String getDisplayName() {
        return "Unused private fields";
    }

    public String getShortName() {
        return "UnusedFields";
    }

    public ProblemDescriptor[] checkClass(PsiClass psiClass, InspectionManager inspectionManager, boolean isOnTheFly) {
        List problems = new ArrayList();
        PsiField[] fields = psiClass.getFields();
        for (int i = 0; i < fields.length; i++) {
            PsiField field = fields[i];
            if (!field.getModifierList().hasModifierProperty("private")) {
                continue;
            }
            if (field.getName().endsWith("Fixture")) {
                continue;
            }
            PsiSearchHelper searchHelper = field.getManager().getSearchHelper();
            SearchScope useScope = field.getUseScope();
            PsiReference[] references = searchHelper.findReferences(field, useScope, true);
            if (references.length == 0) {
                problems.add(
                        inspectionManager.createProblemDescriptor(
                                field.getNameIdentifier(),
                                "Private field '" + field.getName() + "' is never used.",
                                new DeleteFieldQuickFix(field),
                                ProblemHighlightType.LIKE_UNUSED_SYMBOL));
            }
        }
        return (ProblemDescriptor[]) problems.toArray(new ProblemDescriptor[problems.size()]);
    }

    public HighlightDisplayLevel getDefaultLevel() {
        return HighlightDisplayLevel.WARNING;
    }

    public boolean isEnabledByDefault() {
        return true;
    }
}

class DeleteFieldQuickFix implements LocalQuickFix {
    private PsiField field;

    public DeleteFieldQuickFix(PsiField field) {
        this.field = field;
    }

    public String getName() {
        return "Remove field '" + field.getName() + "'";
    }

    public void applyFix(Project project, ProblemDescriptor problemdescriptor) {
        try {
            field.delete();
        } catch (IncorrectOperationException e) {
            throw new RuntimeException(e);
        }
    }

    public String getFamilyName() {
        return "PurpleMonkeyDishwasher";
    }
}
